//
//  HENurseViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/25.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HENurseViewController.h"
#import "HEOrgModel.h"
#import "HENurseOrgCell.h"
#import "HERealNameAuthAlterController.h"
#import "HEHealthArchiveModel.h"
#import "HEMiniTools.h"

@interface HENurseViewController ()<UITableViewDelegate, UITableViewDataSource>
 
 
@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray <HEOrgModel *> * dataSourceArr;

@end

@implementation HENurseViewController

- (NSMutableArray<HEOrgModel *> *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"护理上门";
    [self initUI];
    [self loadData];
    
    [self setCustomTableView:_tableView];
}

- (void)initUI {
    
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
//    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.separatorColor = KHexColor(@"EEEEEE");

    _tableView.scrollsToTop = YES;
    _tableView.tableFooterView = [[UIView alloc] init];
    
    self.tableView.backgroundColor = KWhiteColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    __weak __typeof(&*self)weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        weakSelf.isFromHeadRefresh = YES;
        [weakSelf loadData];
    }];
 
 
    [_tableView registerClass:[HENurseOrgCell class] forCellReuseIdentifier: KHENurseOrgCell];
}

- (void)loadData {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KOrgServer parameters:@{@"server_type": @1} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        
        if (self.isFromHeadRefresh) {
            [self.dataSourceArr removeAllObjects];
        }
        
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArr addObjectsFromArray:[HEOrgModel mj_objectArrayWithKeyValuesArray:response.data]];
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
 
        if (self.dataSourceArr.count == 0) {
            [self showEmpty];
        }

    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
     }];
}



#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HENurseOrgCell *cell = [tableView dequeueReusableCellWithIdentifier: KHENurseOrgCell];
    cell.model = _dataSourceArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView fd_heightForCellWithIdentifier: KHENurseOrgCell cacheByIndexPath:indexPath configuration:^(HENurseOrgCell *cell) {
        cell.model = self.dataSourceArr[indexPath.row];
    }];
}

- (void)loadverifyInfoData:(HEOrgModel *)orgModel {
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KgetPhrUserInfo parameters:@{} success:^(id  _Nonnull responseObject) {
        NSLog(@"response：  %@", [responseObject mj_JSONString]);
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            HEHealthArchiveModel *model = [HEHealthArchiveModel mj_objectWithKeyValues:response.data];
            
            [self loadUserIdCard: orgModel];
        }

    } failure:^(NSError * _Nonnull error) {
     }];
}

- (void)loadUserIdCard:(HEOrgModel *)orgModel {
    KWeakSelf
    [WOAFNetWorkingHandle GETWithBaseUrl:KServerUrl suffixUrl:KuserSfz parameters:@{} success:^(id  _Nonnull responseObject) {
        HEObjectDataResponse *response = [HEObjectDataResponse mj_objectWithKeyValues:responseObject];
        
        if (response.success) {
            
            UserInfo *user = [[UserManager sharedUserManager] getUserInfo];

            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            NSMutableDictionary *patientData = [NSMutableDictionary dictionary];
            [patientData setValue:user.id forKey:@"id"];
            [patientData setValue:user.username forKey:@"phone"];
            [patientData setValue:[NSNumber numberWithInteger:user.sex] forKey:@"sex"];
            [patientData setValue:user.realname forKey:@"userName"];
            [patientData setValue:response.data forKey:@"idCard"];

            [params setValue:patientData forKey:@"patientData"];
 
            NSString *encodeData = [[params mj_JSONString] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            encodeData = [[params mj_JSONString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSString *path = [NSString stringWithFormat:@"%@%@%@&id=%@&name=%@", orgModel.org_callurl, @"?source=app&query=", encodeData, orgModel.Kparams[@"id"], orgModel.Kparams[@"name"]];

            [HEMiniTools jumpToWximin:path username:orgModel.org_init_appid];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEOrgModel *model = _dataSourceArr[indexPath.row];
    
    UserInfo *user = [[UserManager sharedUserManager] getUserInfo];
    
    if (user.verifyStatus) {
        [self loadverifyInfoData: model];
    }else {
        HERealNameAuthAlterController *vc = [HERealNameAuthAlterController new];
        [self.view addSubview:vc.view];
    }
    
 
    /*
    参数说明：
     userName：拉起的小程序的username-原始ID，这个在小程序->设置中可以看见，必须 必须 必须 以 （gh） 开头的 重要的事情说3遍
    **/
//    launchMiniProgramReq.path ="page/index?key1=xxxx";//类似http的url方法来传递参数，如果不传，默认进入小程序首页！
}
@end
